<template>
  <div class="header-btns">
    <RPButton style="margin-right: 10px" v-if="ifProcess" optType="create" type="info" @click="goProcess" text="查看流程" />
    <RPButton optType="reset" style="margin-right: 10px" @click="hide" text="取消" />
    <RPButton style="margin-right: 10px" v-if="ifSave" :disabled="disabledAction" optType="create" type="info" @click="save" text="保存" />
    <RPButton style="margin-right: 10px" v-if="ifSubmit" :disabled="disabledAction || submitDisabled" optType="create" type="info" @click="submit" text="提交" />
    <RPButton style="margin-right: 10px" v-if="ifModify" :disabled="disabledAction" optType="create" type="info" @click="modify" text="重新提交" />
    <RPButton style="margin-right: 10px" v-if="ifApprove" :disabled="disabledAction" optType="create" type="info" @click="approve" text="审批" />
  </div>
</template>
<script>
import {
  PROCESS_STATUS_CREATING,
  PROCESS_STATUS_WAIT_SUBMIT,
  PROCESS_STATUS_WAIT_APPROVE,
  PROCESS_STATUS_APPROVED,
  PROCESS_STATUS_REJECTED,
  SUBMIT_FLAG_YES,
} from '@/tools/common.js'
export default {
  name: 'ProcessButtons',
  props: {
    disabledPage: {
      type: Boolean,
      default: false
    },
    disabledAction: {
      type: Boolean,
      default: true
    },
    fromPage: {
      type: String,
      default: 'creater',
    },
    dataStatus: {
      type: Number,
      default: 0,
    },
    bindId: {
      type: String,
      default: '',
    },
    writePermission: {
      type: Boolean,
      default: true
    },
    submitFlag: {
      type: String,
      default: '',
    },
    oldDataFlag: {
      type: String,
      default: '0',
    },
    rejectFlag: {
      type: String,
      default: '0',
    },
    sourceAction: {
      type: String,
      default: '',
    },
  },
  computed: {
    // 保存
    ifSave() {
      return this.sourceAction == 'midtermEdit' || (!this.disabledPage &&
        ((this.fromPage == 'creater' && (this.dataStatus == PROCESS_STATUS_CREATING || this.dataStatus == PROCESS_STATUS_WAIT_SUBMIT || this.dataStatus == PROCESS_STATUS_REJECTED)) ||
        (this.fromPage == 'Summary' && this.dataStatus == PROCESS_STATUS_APPROVED)))
    },
    // 提交
    ifSubmit() {
      return !this.disabledPage && this.sourceAction != 'midtermEdit' &&
        this.fromPage == 'creater' && (this.dataStatus == PROCESS_STATUS_CREATING || this.dataStatus == PROCESS_STATUS_WAIT_SUBMIT);
    },
    // 提交修改
    ifModify() {
      return !this.disabledPage && this.sourceAction != 'midtermEdit' &&
        this.fromPage == 'creater' && this.dataStatus == PROCESS_STATUS_REJECTED;
    },
    // 审批
    ifApprove() {
      return this.submitFlag === SUBMIT_FLAG_YES && this.fromPage == 'Approver' && this.sourceAction != 'midtermEdit' &&
        this.oldDataFlag == '1' &&
        this.dataStatus == PROCESS_STATUS_WAIT_APPROVE;
    },
    // 查看流程
    ifProcess() {
      return !!this.bindId && this.oldDataFlag == '0' && this.sourceAction != 'midtermEdit';
    },
    // saveDisabled() {
    //   if (this.writePermission === false && this.rejectFlag != '1') {
    //     return true
    //   } else {
    //     return false
    //   }
    // },
    submitDisabled() {
      if (this.writePermission === false && this.rejectFlag != '1') {
        return true
      } else {
        return false
      }
    },
  },
  methods:{
    hide() {
      this.$emit('hide')
    },
    save() {
      this.$emit('save', PROCESS_STATUS_WAIT_SUBMIT)
    },
    submit() {
      this.$emit('submit', PROCESS_STATUS_WAIT_APPROVE)
    },
    modify() {
      this.$emit('modify', PROCESS_STATUS_WAIT_APPROVE)
    },
    withdraw() {
      this.$emit('withdraw', PROCESS_STATUS_WAIT_SUBMIT)
    },
    approve() {
      this.$emit('approve', true)
    },
    reject() {
      this.$emit('reject', false)
    },
    goProcess() {
      this.$emit('process')
    }
  }
}
</script>
